<?php
/**
* @version $Id: install.sobi2.php 3825 2008-03-19 09:36:14Z Radek Suski $
* @package: Sigsiu Online Business Index 2
* ===================================================
* @author
* Name: Sigrid & Radek Suski, Sigsiu.NET
* Email: sobi@sigsiu.net
* Url: http://www.sigsiu.net
* ===================================================
* @copyright Copyright (C) 2007 Sigsiu.NET (http://www.sigsiu.net). All rights reserved.
* @license see http://www.gnu.org/licenses/lgpl.html GNU/LGPL.
* You can use, redistribute this file and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
* 11 03 2008
*/


defined( '_VALID_MOS' ) || ( trigger_error("Restricted access", E_USER_ERROR) && exit() );
define("_SOBI2_V", '$Revision: 3825 $');

function get_include_contents($filename)
{
   if (is_file($filename)) {
       ob_start();
       include $filename;
       $contents = ob_get_contents();
       ob_end_clean();
       return $contents;
   }
   return false;
}

function com_install()
{
	global $database, $mosConfig_absolute_path,$mosConfig_lang;
	@ini_set('max_execution_time', '180');
	$msg = installSobiData();

	if(!defined("DS")) {
		define("DS",DIRECTORY_SEPARATOR);
	}
	if(file_exists($mosConfig_absolute_path.DS."administrator".DS."includes".DS."pcl".DS."pclzip.lib.php")) {
		require_once($mosConfig_absolute_path.DS."administrator".DS."includes".DS."pcl".DS."pclzip.lib.php");
	}

	$archivename = $mosConfig_absolute_path.DS."administrator".DS."components".DS."com_sobi2".DS."includes".DS."install".DS."crystalsvg.zip";
	$zipfile = new PclZip($archivename);
	$zipfile->extract(PCLZIP_OPT_PATH, $mosConfig_absolute_path.DS."images".DS."stories".DS);

	$archivename = $mosConfig_absolute_path.DS."administrator".DS."components".DS."com_sobi2".DS."includes".DS."install".DS."langs.zip";
	$zipfile = new PclZip($archivename);
	$zipfile->extract(PCLZIP_OPT_PATH, $mosConfig_absolute_path.DS."administrator".DS."components".DS."com_sobi2".DS."languages".DS);

	@unlink($mosConfig_absolute_path.DS."images".DS."stories".DS."folder_add_f2.png");
	@unlink($mosConfig_absolute_path.DS."images".DS."stories".DS."properties_f2.png");

	@chdir($mosConfig_absolute_path.DS."images".DS);
	@mkdir("com_sobi2", 0777);
	@chdir($mosConfig_absolute_path.DS.images.DS."com_sobi2".DS);
	@mkdir("clients", 0777);

	if(file_exists($mosConfig_absolute_path.DS."administrator".DS."components".DS."com_sobi2".DS."includes".DS."about".DS.$mosConfig_lang.".html")) {
		$msg .= get_include_contents($mosConfig_absolute_path.DS."administrator".DS."components".DS."com_sobi2".DS."includes".DS."about".DS.$mosConfig_lang.".html");
	}
	else {
		$msg .=  get_include_contents($mosConfig_absolute_path.DS."administrator".DS."components".DS."com_sobi2".DS."includes".DS."about".DS."english.html");
	}
	return $msg;
}
function installSobiData()
{
	global $database;
	$msg = null;

	$database->setQuery( "UPDATE #__components SET admin_menu_img = '../administrator/components/com_sobi2/images/sobi_sm.jpg' WHERE admin_menu_link='option=com_sobi2'");
	$database->query();

	$msg .= '<br/><big style="font-weight: bold; color: rgb(255, 0, 0);"><big><tt>admin menu updated ... </tt></big></big><br/>';

	$query = "SELECT `id` FROM #__components WHERE `option` = 'com_sobi2'";
	$database->setQuery( $query );
	$id = $database->loadResult();

	$query = "UPDATE `#__menu` SET `componentid` = {$id} WHERE link LIKE 'index.php?option=com_sobi2%'";
	$database->setQuery( $query );
	$database->query();

	$query = "SELECT COUNT(*) FROM #__sobi2_fields";
	$database->setQuery($query);
	if( $database->loadResult() ) {
		$msg .= '<big style="font-weight: bold; color: rgb(255, 0, 0);"><big><tt>menu entries fixed ....</tt></big></big><br/>';

		$query = "SELECT `configValue` FROM #__sobi2_config WHERE `configKey` = 'version' AND `sobi2Section` = 'version'";
		$database->setQuery($query);
		$ver = $database->loadResult();
		$ver = ereg_replace("[^0-9]","",$ver);

		$query = "ALTER TABLE `#__sobi2_language` CHANGE `langValue` `langValue` VARCHAR( 255 ) ";
		$database->setQuery($query);
		$database->query();

		$query = "ALTER TABLE `#__sobi2_item` CHANGE `title` `title` VARCHAR( 255 )";
		$database->setQuery($query);
		$database->query();

		if( $ver < 3400 ) {
			$query = "ALTER TABLE `#__sobi2_fields_data` ADD INDEX `field_item` ( `fieldid` , `itemid` ) ";
			$database->setQuery($query);
			$database->query();
		}
		$query = 'UPDATE `#__sobi2_config` SET `configValue` = \''._SOBI2_V.'\' WHERE `configKey` = \'version\' AND `sobi2Section` = \'version\' LIMIT 1 ;';
		$database->setQuery( $query );
		$database->query();
		if( $ver < 3400 ) {
			$msg .= '<br/><big style="font-weight: bold; font-size: 18px; color: rgb(255, 0, 0);"><big><tt>You have updated SOBI2 from an older version. Please notice that since the version RC 2.8.3 the template files are stored in another directory ( components/com_sobi2/templates/default ). So if you need to restore your old templates please copy these template files to the new location. If you edit the templates via the template editor in the admin panel, the templates will be automatically saved to the right place. </tt></big></big><br/>';
		}
		return $msg;
	}

	/* fields */
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (1, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (2, 1, 0, NULL, NULL, 1, 0, 10, 0, 0, 30, 'inputbox', 1, 0, 1, 0, 1, 1, 2, 2, 0, 1, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (3, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 0, 1, 0, 1, 1, 3, 2, 0, 0, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (4, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 0, 1, 0, 1, 1, 4, 2, 0, 1, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (5, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 0, 1, 0, 1, 1, 5, 2, 0, 1, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (6, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 0, 1, 0, 1, 1, 6, 2, 0, 0, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (7, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 0, 1, 0, 0, 1, 7, 0, 1, 1, 2, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (8, 1, 0, NULL, NULL, 0, 10, 100, 0, 0, 30, 'inputbox', 1, 1, 0, 0, 1, 1, 8, 0, 0, 1, 1, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (9, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 1, 0, 0, 0, 1, 9, 1, 1, 1, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (10, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 1, 0, 0, 0, 1, 10, 1, 1, 1, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (11, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 1, 0, 0, 0, 1, 11, 1, 1, 1, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (12, 1, 0, NULL, NULL, 1, 0, 100, 0, 0, 30, 'inputbox', 1, 1, 0, 0, 0, 1, 12, 1, 1, 1, 0, 0, '0000-00-00 00:00:00', 0);";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_fields VALUES (13, 2, 1, NULL, NULL, 0, 25, 0, 20, 50, 0, 'inputbox', 1, 1, 0, 0, 0, 1, 13, 1, 0, 1, 0, 0, '0000-00-00 00:00:00', 0);";

	/* categories */
	$installQueries[] = "INSERT IGNORE INTO `#__sobi2_categories` VALUES (1, 'root category', 'articles.jpg', 'right', 'Sigsiu Online Business Index 2', '', 0, 0, '0000-00-00 00:00:00', NULL, NULL, 0, NULL, '');";
	$installQueries[] = "INSERT IGNORE INTO `#__sobi2_categories` VALUES (2, 'First Category', '', 'left', 'First Category Description', 'First Category Introtex', 1, 0, '0000-00-00 00:00:00', 1, 0, 0, NULL, 'folder_red_open.png');";
	$installQueries[] = "INSERT IGNORE INTO `#__sobi2_cats_relations` VALUES (2, 1);";

	/* labels */
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_street', 'Street', '', 'fields', 1, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_postcode', 'Postcode', '', 'fields', 2, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_city', 'City', '', 'fields', 3, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_county', 'County', '', 'fields', 4, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_federal_state', 'Federal State', '', 'fields', 5, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_country', 'Country', '', 'fields', 6, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_email', 'Email', '', 'fields', 7, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_website', 'Website', '', 'fields', 8, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_contact_person', 'Contact Person', '', 'fields', 9, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_phone', 'Phone', '', 'fields', 10, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_fax', 'Fax', '', 'fields', 11, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_hotline', 'Hotline', '', 'fields', 12, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_description', 'Description', '', 'fields', 13, 'english');";

	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_street', 'Strasse', '', 'fields', 1, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_postcode', 'PLZ', '', 'fields', 2, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_city', 'Stadt', '', 'fields', 3, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_county', 'Kreis', '', 'fields', 4, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_federal_state', 'Bundesland', '', 'fields', 5, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_country', 'Land', '', 'fields', 6, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_email', 'Email', '', 'fields', 7, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_website', 'Website', '', 'fields', 8, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_contact_person', 'Kontaktperson', '', 'fields', 9, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_phone', 'Telefon', '', 'fields', 10, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_fax', 'Fax', '', 'fields', 11, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_hotline', 'Hotline', '', 'fields', 12, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_description', 'Beschreibung', '', 'fields', 13, 'germani');";

	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_street', 'Strasse', '', 'fields', 1, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_postcode', 'PLZ', '', 'fields', 2, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_city', 'Stadt', '', 'fields', 3, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_county', 'Kreis', '', 'fields', 4, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_federal_state', 'Bundesland', '', 'fields', 5, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_country', 'Land', '', 'fields', 6, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_email', 'Email', '', 'fields', 7, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_website', 'Website', '', 'fields', 8, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_contact_person', 'Kontaktperson', '', 'fields', 9, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_phone', 'Telefon', '', 'fields', 10, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_fax', 'Fax', '', 'fields', 11, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_hotline', 'Hotline', '', 'fields', 12, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('field_description', 'Beschreibung', '', 'fields', 13, 'germanf');";


	/* emails */
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_submit_title', '', 'Your entry in {sobi} on {sitename}', 'emails', null, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_submit_text', '', 'Your entry in {sobi} on {sitename} was added and awaiting approval.', 'emails', null, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_update_title', '', 'Your entry in {sobi} on {sitename} has been updated', 'emails', null, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_update_text', '', 'Your entry in {sobi} on {sitename} has been updated. \n {link_details}', 'emails', null, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_approve_title', '', 'Your entry in {sobi} on {sitename} has been approved', 'emails', null, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_approve_text', '', 'Your entry in {sobi} on {sitename} has been approved. \n {link_details}', 'emails', null, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_payments_title', '', 'Your entry in {sobi} on {sitename} (Payments details)', 'emails', null, 'english');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_payments_text', '', '\nThank you for your interest in our products.\n-----------------------\n\nYou added an entry in: {sobi} on {sitename} \n\nYou have chosen the following not free options: \n{selected_options} \n\nTotal Amount: {total} \nPlease send the money to the following account: \n{bank_data} or pay with PayPal: \n{paypal_url}', 'emails', null, 'english');";

	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_submit_title', '', 'Ihr Eintrag in {sobi} auf {sitename}', 'emails', null, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_submit_text', '', 'Ihr Eintrag in {sobi} auf {sitename} wurde hinzugef??gt und wartet auf Genehmigung.', 'emails', null, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_update_title', '', 'Ihr Eintrag in {sobi} auf {sitename} wurde aktualisiert', 'emails', null, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_update_text', '', 'Ihr Eintrag in {sobi} auf {sitename} wurde aktualisiert. \n {link_details}', 'emails', null, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_approve_title', '', 'Ihr Eintrag in {sobi} auf {sitename} wurde genehmigt', 'emails', null, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_approve_text', '', 'Ihr Eintrag in {sobi} auf {sitename} wurde genehmigt. \n {link_details}', 'emails', null, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_payments_title', '', 'Ihr Eintrag in {sobi} auf {sitename} (Zahlungshinweise)', 'emails', null, 'germanf');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_payments_text', '', '\nVielen Dank f??r Ihr Interesse an unserem Service.\n-----------------------\n\nSie haben auf {sitename} einen Eintrag in {sobi} hinzugef??gt. \n\nSie haben die folgenden kostenpflichtigen Optionen gew??hlt: \n{selected_options} \n\nSumme: {total} \nBitte ??berweisen Sie den Betrag auf folgendes Bankkonto: \n{bank_data} \nOder zahlen Sie per PayPal: \n{paypal_url}', 'emails', null, 'germanf');";

	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_submit_title', '', 'Dein Eintrag in {sobi} auf {sitename}', 'emails', null, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_submit_text', '', 'Dein Eintrag in {sobi} auf {sitename} wurde hinzugef??gt und wartet auf Genehmigung.', 'emails', null, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_update_title', 'Dein Eintrag in {sobi} auf {sitename} wurde aktualisiert', '', 'emails', null, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_update_text', '', 'Dein Eintrag in {sobi} auf {sitename} wurde aktualisiert. \n {link_details}', 'emails', null, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_approve_title', '', 'Dein Eintrag in {sobi} auf {sitename} wurde genehmigt', 'emails', null, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_on_approve_text', '', 'Dein Eintrag in {sobi} auf {sitename} wurde genehmigt. \n {link_details}', 'emails', null, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_payments_title', '', 'Dein Eintrag in {sobi} auf {sitename} (Zahlungshinweise)', 'emails', null, 'germani');";
	$installQueries[] = "INSERT IGNORE INTO #__sobi2_language VALUES ('email_payments_text', '', '\nVielen Dank f??r Dein Interesse an unserem Service.\n-----------------------\n\nDu hast auf {sitename} einen Eintrag in {sobi} hinzugef??gt. \n\nDu hast die folgenden kostenpflichtigen Optionen gew??hlt: \n{selected_options} \n\nSumme: {total} \nBitte ??berweise den Betrag auf folgendes Bankkonto: \n{bank_data}\nOder zahle per PayPal: \n{paypal_url}', 'emails', null, 'germani');";

	$msg .= '<big style="font-weight: bold; color: rgb(255, 0, 0);"><big><tt>Installing data ....</tt></big></big><br/>';
	foreach ($installQueries as $query) {
		$database->setQuery($query);
		$database->query();
	}
}
?>
